package com.mygdx.game.Screens;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.B2WorldCreator;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.Scenes.Hud;
import com.mygdx.game.Sprite.Mario;
import com.mygdx.game.Sprite.NPC;
import com.mygdx.game.Stage.DialogueStage;


public class HomeScreen implements Screen {
    public static MyGdxGame game;
    public static int stageFlag = 0;

    private OrthographicCamera gamecam;
    private Viewport gamePort;
    public static Mario mario;
    private Hud hud;


    private TmxMapLoader mapLoader;
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;

    private DialogueStage dialogueStage;

    //Box2d
    private World world;
    private Box2DDebugRenderer b2dr;

    public HomeScreen(final MyGdxGame game) {
        this.game = game;

        gamecam = new OrthographicCamera();
        gamePort = new FitViewport(MyGdxGame.V_WIDTH , MyGdxGame.V_HEIGHT , gamecam);
        hud = new Hud(game.batch,910,495);
        mapLoader = new TmxMapLoader();
        map = mapLoader.load("home.tmx");
        renderer = new OrthogonalTiledMapRenderer(map);
        gamecam.position.set(gamePort.getScreenWidth()/2,gamePort.getScreenHeight()/2,0);
        world = new World(new Vector2(0, 0), true);
       // dialogueStage = new DialogueStage("image/story.png",MyGdxGame.V_WIDTH/2,MyGdxGame.V_HEIGHT/2);
        world.setContactListener(new ContactListener() {
            @Override
            public void beginContact(Contact contact) {

                System.out.println(gamecam.position);
                if(gamecam.position.x>578 && gamecam.position.x<645 && gamecam.position.y>20 && gamecam.position.y<43)
                {
                    PlayScreen.flag=1;
                    PlayScreen.positionFlag=1;
                    game.setScreen(new PlayScreen(game));
                    dispose();

                }
                if(gamecam.position.x>1010 && gamecam.position.x<1149 && gamecam.position.y>578 && gamecam.position.y<735)
                {
                    stageFlag = 1;
                }

            }

            @Override
            public void endContact(Contact contact) {

            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {

            }
        });
        b2dr = new Box2DDebugRenderer();
        new B2WorldCreator(world,map,game);
        mario = new Mario(world,2,600,55);
//        mario.b2Body.setUserData(dialogueStage.getActors());




    }
    public void handleInput(float dt) {
        if (Gdx.input.isKeyPressed(Input.Keys.UP) && mario.b2Body.getLinearVelocity().y<=200f) {
            mario.b2Body.applyLinearImpulse(new Vector2(0, 20f), mario.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && mario.b2Body.getLinearVelocity().x<=200f) {
            mario.b2Body.applyLinearImpulse(new Vector2(20f, 0), mario.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN) && mario.b2Body.getLinearVelocity().y>=-200f) {
            mario.b2Body.applyLinearImpulse(new Vector2(0, -20f), mario.b2Body.getWorldCenter(), true);
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && mario.b2Body.getLinearVelocity().x>=-200f) {
            mario.b2Body.applyLinearImpulse(new Vector2(-20f, 0), mario.b2Body.getWorldCenter(), true);
        } else {
//
        }

    }
    public void update(float dt) {
        handleInput(dt);
        world.step(1 / 60f, 6, 2);
        //摄像头跟随人物底层图形
        gamecam.position.x = mario.b2Body.getPosition().x;
        gamecam.position.y = mario.b2Body.getPosition().y;
        gamecam.update();
        renderer.setView(gamecam);
    }

    @Override
    public void show() {

    }
    @Override
    public void render(float delta) {
        update(delta);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        renderer.render();
        b2dr.render(world, gamecam.combined);


        hud.stage.draw();
        selectStage();

        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            System.out.println("exit");
            Gdx.app.exit();
        }


    }
    public void selectStage(){
        switch (stageFlag) {
            case 1: {
//                //dialog
//                Gdx.input.setInputProcessor(dialogueStage);
//                dialogueStage.draw();
//                if (dialogueStage.getClick_Count()==3)
//                dialogueStage.getRoot().removeActor(dialogueStage.getactor());
//
            }
            break;


        }
        if (Gdx.input.isKeyPressed(Input.Keys.BACKSPACE)) {
            switch (stageFlag) {
                case 1: {
                    stageFlag = 0;
                }
                break;
                case 2: {
                    stageFlag = 1;
                }
                break;
                case 3: {
                    stageFlag = 2;
                }
                break;
            }
        }
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            System.out.println("exit");
            Gdx.app.exit();
        }

    }
    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);
    }
    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}
